﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// 地图编辑器的主界面
/// </summary>

public class MainWindow : ScriptableWizard
{

    public delegate void OnSelectionCallback(Object obj);
    public static MainWindow instance;
    public static MapConfigModel mcm;
    public static Object Data;
    static Vector3 cameraInterval = new Vector3(-0.1f, 9.54f, -9.07f);
    OnSelectionCallback mCallback;
    Object[] mObjects;
    Vector2 mScroll = Vector2.zero;
    string path = "Assets/SysData.prefab";
    public static GameObject MissionInfo;
    static MissionInfo info;
    private string mapID;
    private GameObject newMapModel;
    private int counts;
    private string otherGameObjectName;
    private int xSpace;
    private int zSpace;
    private int monsterId;
    private int monsterDropId;
    private int breadOutId;
    void OnEnable()
    {
        mapID = "";
        otherGameObjectName = "";
        MissionInfo = null;
        instance = this;
        MissionInfo = GameObject.Find("MissionSystem");
        #region 加载数据
        if (MissionInfo != null)
        {
            mcm = ScriptableObject.CreateInstance<MapConfigModel>(); ;// new MapConfigModel();
            info = MissionInfo.GetComponent<MissionInfo>();
            mcm.SideList = info.SideList;
            mcm.MapStege = info.MapStege;
            mcm.ListPointTranfrom = info.ListPointTranfrom;
            mcm.OtherGameObject = info.OtherGameObject;
            mcm.MapID = info.MapID;
            mcm.ListFightingType = info.ListFightingType;
            mcm.MonsterPointPanel = info.MonsterPointPanel;
            mcm.MapPanel = info.MapPanel;
            mcm.HideList = info.HidList;
            mcm.OtherGameObjectPrefab = info.OtherGameObjcetPrefabList;
            mcm.SidePrefab = info.SidePrefabList;
            mcm.ListMonsterSpawnEnum = info.ListMonsterSpawnEnum;
            GameObject SystemInit = GameObject.Find("SystemInit");
            if (SystemInit == null)
            {
                SystemInit = new GameObject("SystemInit");
                SystemInit.AddComponent<Behaviour>();
                SystemInit.AddComponent<TouchingObserver>();
                DemoInit init = SystemInit.AddComponent<DemoInit>();
                init.Mission = info;
            }
        }
        else
        {
            MissionInfo = new GameObject("MissionSystem");
            info = MissionInfo.AddComponent<MissionInfo>();
            mcm = ScriptableObject.CreateInstance<MapConfigModel>();
            //GameObject SystemInit = GameObject.Find("SystemInit");
            //if (SystemInit == null)
            //{
            //    SystemInit = new GameObject("SystemInit");
            //    SystemInit.AddComponent<Behaviour>();
            //    SystemInit.AddComponent<TouchingObserver>();
            //    DemoInit init = SystemInit.AddComponent<DemoInit>();
            //    init.Mission = info;
            //}
            info.MapStege = mcm.MapStege;
            info.SideList = mcm.SideList;
            info.ListPointTranfrom = mcm.ListPointTranfrom;
            info.ListFightingType = mcm.ListFightingType;
            info.MapID = mcm.MapID;
            info.MapPanel = mcm.MapPanel;
            info.MonsterPointPanel = mcm.MonsterPointPanel;
            info.OtherGameObject = mcm.OtherGameObject;
            info.HidList = mcm.HideList;
            info.OtherGameObjcetPrefabList = mcm.OtherGameObjectPrefab;
            info.SidePrefabList = mcm.SidePrefab;
            info.ListMonsterSpawnEnum = mcm.ListMonsterSpawnEnum;

        }

        #endregion
        #region 加载数据
        //if (mcm == null || mcm.MapStege == null)
        //{

        //}
        #endregion
    }
    void OnDisable()
    {
        mcm = null;
        SaveDate();
        instance = null;
    }

    void OnGUI()
    {
        #region 新建关卡 功能

        GUILayout.BeginHorizontal();
        GUILayout.Label("关卡地图模型", GUILayout.Width(120f));
        newMapModel = EditorGUILayout.ObjectField(newMapModel, typeof(GameObject), true, GUILayout.Width(200)) as GameObject;
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("输入关卡ID，新建关卡", GUILayout.Width(120f));
        mapID = GUILayout.TextField(mapID, 20, GUILayout.Width(100f));

        EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(mapID) || newMapModel == null);
        if (GUILayout.Button("添加小关卡", GUILayout.Width(100f)))
        {
            string rolePath = "role/Sylvanas";
            GameObject missionRolePoint = Instantiate(Resources.Load(rolePath)) as GameObject;
            missionRolePoint.name = "RolePoint";
            GameObject newMap = newMapModel;
            GameObject missionPartPanel = new GameObject("map" + mapID);
            MapInfo mapInfo = missionPartPanel.AddComponent<MapInfo>();
            GameObject missionSidePanel = new GameObject("SidePanel");
            GameObject missionMonsterPanel = new GameObject("MonsterPanel");
            GameObject missionOtherPanel = new GameObject("OtherPanel");

            mcm.AddItem(mapID, missionPartPanel, missionSidePanel, missionRolePoint, missionMonsterPanel, missionOtherPanel, newMap);
            missionPartPanel.transform.transform.localPosition = newMapModel.transform.position;
            //missionPartPanel.transform.position -= new Vector3(0, newMapModel.transform.position.y, 0);
            missionSidePanel.transform.parent = missionPartPanel.transform;
            missionSidePanel.transform.localPosition = Vector3.zero;
            missionRolePoint.transform.parent = missionPartPanel.transform;
            missionRolePoint.transform.localPosition = Vector3.zero;
            missionMonsterPanel.transform.parent = missionPartPanel.transform;
            missionMonsterPanel.transform.localPosition = Vector3.zero;
            missionOtherPanel.transform.parent = missionPartPanel.transform;
            missionOtherPanel.transform.localPosition = Vector3.zero;
            Reflash(mapInfo, 0);

            mapID = (int.Parse(mapID) + 1) + "";
            newMapModel = null;
        }
        GUILayout.Label("关卡ID和关卡地图模型不能为null，才能新建关卡");
        EditorGUI.EndDisabledGroup();
        GUILayout.EndHorizontal();
        GUILayout.Space(20);
        #endregion

        #region 关卡列表
        mScroll = GUILayout.BeginScrollView(mScroll);
        if (mcm.MapStege.Count > 0)
        {
            for (int i = 0; i < mcm.MapID.Count; i++)
            {
                if (mcm.MapPanel.Count <= i || mcm.MapPanel[i] == null)
                {
                    //DestroyImmediate(mcm.MapPanel[i]);
                    mcm.MapID.RemoveAt(i);
                    mcm.Remove(i);
                }
                MapInfo mapInfo = mcm.MapPanel[i].GetComponent<MapInfo>();
                #region 小地图的ID
                GUILayout.BeginHorizontal();
                {
                    #region 折叠
                    if (GUILayout.Button(mcm.HideList[i] == true ? "展开+" : "折叠-", GUILayout.Width(50f)))
                    {
                        mcm.HideList[i] = !mcm.HideList[i];
                    }
                    #endregion
                    GUILayout.Label("本关卡ID", GUILayout.Width(50f));
                    string mapid = mcm.MapID[i];
                    GUILayout.TextField(mapid, 20, GUILayout.Width(100f));
                    EditorGUI.BeginDisabledGroup(mcm.MapID[i] == null);
                    if (GUILayout.Button("移除本关卡", GUILayout.Width(100f)))
                    {
                        DestroyImmediate(mcm.MapPanel[i]);
                        mcm.Remove(i);
                        return;
                    }
                    EditorGUI.EndDisabledGroup();

                }
                GUILayout.EndHorizontal();
                #endregion

                #region 本关卡大地图
                GUILayout.BeginHorizontal();
                {
                    if (mcm.MapStege == null)
                    {
                        OnEnable();
                    }
                    GameObject map = mcm.MapStege[i];
                    AddItem(map, "本关卡地图模型", mcm.MapStege, i);
                    //EditorGUI.BeginDisabledGroup(mcm.MapStege[i] == null);
                    //if (GUILayout.Button("移除", GUILayout.Width(40f))) { mcm.MapStege[i] = null; }
                    //EditorGUI.EndDisabledGroup();
                }
                GUILayout.EndHorizontal();
                #endregion

                #region 关卡节点顶层
                GUILayout.BeginHorizontal();
                GUILayout.Label("本关卡节点:", GUILayout.Width(100));
                mcm.MapPanel[i] = EditorGUILayout.ObjectField(mcm.MapPanel[i], typeof(GameObject), true, GUILayout.Width(100)) as GameObject;
                GUILayout.EndHorizontal();
                #endregion

                #region 关卡战斗类型
                GUILayout.BeginHorizontal();
                GUILayout.Label("本关卡战斗类型:", GUILayout.Width(100f));
                mcm.ListFightingType[i] = (Def.FightingType)EditorGUILayout.EnumPopup(mcm.ListFightingType[i], GUILayout.Width(150));

                GUILayout.Label("通关时间:", GUILayout.Width(50f));
                mapInfo.MissionTimes = EditorGUILayout.FloatField(mapInfo.MissionTimes, GUILayout.Width(50));

                switch (mcm.ListFightingType[i])
                {
                    case Def.FightingType.Kill:
                        GUILayout.Label("Boss移动目标点:", GUILayout.Width(100f));
                        mapInfo.BossTargertPositon = EditorGUILayout.ObjectField(mapInfo.BossTargertPositon, typeof(Transform), true, GUILayout.Width(150)) as Transform;
                        break;
                    case Def.FightingType.Protect:
                        GUILayout.Label("保护的物品:", GUILayout.Width(60));
                        //if (mapInfo.ProtectObjcet == null)
                        {
                            mapInfo.ProtectObjcet = EditorGUILayout.ObjectField(mapInfo.ProtectObjcet, typeof(GameObject), true, GUILayout.Width(120)) as GameObject;
                        }
                        GUILayout.Label("最大血量:", GUILayout.Width(50));
                        mapInfo.ProtectObjectHp = EditorGUILayout.IntField(mapInfo.ProtectObjectHp, GUILayout.Width(100));
                        EditorGUI.BeginDisabledGroup(mapInfo.ProtectObjcet == null || mapInfo.ProtectObjcet.name == "ProtectObjcet");
                        if (GUILayout.Button("生成", GUILayout.Width(100)))
                        {
                            if (mapInfo.UnMoveObjcet != null) { DestroyImmediate(mapInfo.UnMoveObjcet); mapInfo.UnMoveObjcetCounts = 0; }
                            mapInfo.ProtectObjcet = Instantiate(mapInfo.ProtectObjcet) as GameObject;
                            mapInfo.ProtectObjcet.transform.parent = mapInfo.transform;
                            mapInfo.ProtectObjcet.transform.localPosition = Vector3.zero;
                            mapInfo.ProtectObjcet.name = "ProtectObjcet";
                            mapInfo.ProtectObjcet.layer = 19;
                            mapInfo.ProtectObjcet.AddComponent<ProtectView>().nowHp = mapInfo.ProtectObjectHp;
                            BoxCollider box = mapInfo.ProtectObjcet.AddComponent<BoxCollider>();
                            box.isTrigger = true;
                        }
                        EditorGUI.EndDisabledGroup();
                        EditorGUI.BeginDisabledGroup(mapInfo.ProtectObjcet == null || mapInfo.ProtectObjcet.name != "ProtectObjcet");
                        if (GUILayout.Button("移除", GUILayout.Width(100)))
                        {
                            DestroyImmediate(mapInfo.ProtectObjcet);
                        }
                        EditorGUI.EndDisabledGroup();
                        break;
                    case Def.FightingType.Hold:
                        GUILayout.Label("守护的地方:", GUILayout.Width(60));
                        //if (mapInfo.UnMoveObjcet == null)
                        {
                            mapInfo.UnMoveObjcet = EditorGUILayout.ObjectField(mapInfo.UnMoveObjcet, typeof(GameObject), true, GUILayout.Width(120)) as GameObject;

                        }
                        GUILayout.Label("允许次数:", GUILayout.Width(50));
                        mapInfo.UnMoveObjcetCounts = EditorGUILayout.IntField(mapInfo.UnMoveObjcetCounts, GUILayout.Width(50));
                        EditorGUI.BeginDisabledGroup(mapInfo.UnMoveObjcet == null || mapInfo.UnMoveObjcet.name == "UnMoveObject");
                        if (GUILayout.Button("生成", GUILayout.Width(100)))
                        {
                            if (mapInfo.ProtectObjcet != null) { DestroyImmediate(mapInfo.ProtectObjcet); mapInfo.ProtectObjectHp = 0; }
                            mapInfo.UnMoveObjcet = Instantiate(mapInfo.UnMoveObjcet) as GameObject;
                            mapInfo.UnMoveObjcet.transform.parent = mapInfo.transform;
                            mapInfo.UnMoveObjcet.transform.localPosition = Vector3.zero;
                            mapInfo.UnMoveObjcet.name = "UnMoveObject";
                            mapInfo.UnMoveObjcet.layer = 20;
                            mapInfo.UnMoveObjcet.AddComponent<HoldObjectView>().entercounts = mapInfo.UnMoveObjcetCounts;
                        }
                        EditorGUI.EndDisabledGroup();
                        EditorGUI.BeginDisabledGroup(mapInfo.UnMoveObjcet == null || mapInfo.UnMoveObjcet.name != "UnMoveObject");
                        if (GUILayout.Button("移除", GUILayout.Width(100)))
                        {
                            DestroyImmediate(mapInfo.UnMoveObjcet);
                        }
                        EditorGUI.EndDisabledGroup();
                        break;
                    default:
                        break;
                }
                GUILayout.EndHorizontal();
                #endregion

                #region 刷怪类型
                GUILayout.BeginHorizontal();
                GUILayout.Label("刷怪类型:", GUILayout.Width(50));
                mcm.ListMonsterSpawnEnum[i] = (Def.MonsterSpawnEnum)EditorGUILayout.EnumPopup(mcm.ListMonsterSpawnEnum[i], GUILayout.Width(90));
                if (mcm.ListMonsterSpawnEnum[i] != Def.MonsterSpawnEnum.OnTrigger)
                {
                    //  MapInfo mapInfo = mcm.MapPanel[i].GetComponent<MapInfo>();
                    GUILayout.Label("刷怪间隔时间:", GUILayout.Width(80));
                    mapInfo.MonsterSpawnInterval = EditorGUILayout.IntField(mapInfo.MonsterSpawnInterval, GUILayout.Width(50));
                    if (mcm.ListMonsterSpawnEnum[i] == Def.MonsterSpawnEnum.Times || mcm.ListMonsterSpawnEnum[i] == Def.MonsterSpawnEnum.OnTriggerAndOther)
                    {
                        GUILayout.Label("刷怪上限:", GUILayout.Width(50));
                        mapInfo.MonsterSpawnMax = EditorGUILayout.IntField(mapInfo.MonsterSpawnMax, GUILayout.Width(50));
                    }
                }
                GUILayout.EndHorizontal();
                #endregion

                #region 出生点
                GUILayout.BeginHorizontal();
                GUILayout.Label("出生点:", GUILayout.Width(100));
                mcm.ListPointTranfrom[i] = EditorGUILayout.ObjectField(mcm.ListPointTranfrom[i], typeof(GameObject), true, GUILayout.Width(120)) as GameObject;
                GUILayout.EndHorizontal();
                #endregion

                #region 摄像机出生点
                GUILayout.BeginHorizontal();
                EditorGUI.BeginDisabledGroup(mapInfo.CameraPoint == true && mapInfo.RolePoint != null);
                if (GUILayout.Button("添加摄像机", GUILayout.Width(100)))
                {
                    GameObject cameraPoint = Instantiate(Resources.Load("Camera")) as GameObject;

                    cameraPoint.transform.position = mapInfo.RolePoint.transform.position + cameraInterval;
                    cameraPoint.transform.parent = mapInfo.transform;
                    cameraPoint.name = "CameraPoint";
                    mapInfo.CameraPoint = cameraPoint;
                }
                EditorGUI.EndDisabledGroup();
                if (mapInfo.CameraPoint != null)
                {
                    GUILayout.Label("摄像机出生点:", GUILayout.Width(100));
                    mapInfo.CameraPoint = EditorGUILayout.ObjectField(mapInfo.CameraPoint, typeof(GameObject), true, GUILayout.Width(120)) as GameObject;
                    if (GUILayout.Button(mapInfo.CameraPoint.activeSelf == true ? "关闭" : "显示", GUILayout.Width(50)))
                    {
                        mapInfo.CameraPoint.SetActive(!mapInfo.CameraPoint.activeSelf);
                    }
                    if (GUILayout.Button("同步出生点", GUILayout.Width(100)))
                    {
                        Debug.Log(mapInfo.RolePoint.transform.position);
                        mapInfo.CameraPoint.transform.position = mapInfo.RolePoint.transform.position + cameraInterval;
                        Debug.Log(mapInfo.CameraPoint.transform.position);
                    }
                }
                GUILayout.EndHorizontal();
                #endregion

                if (mcm.HideList[i] == false)            //折叠
                {
                    #region 边界遮挡物
                    GUILayout.BeginHorizontal();
                    GameObject side = mcm.SideList[i];
                    AddItem(side, "边界遮挡物", mcm.SideList, i);
                    GUILayout.Label("边界遮挡物预设", GUILayout.Width(100f));
                    GameObject sidePrefab = mcm.SidePrefab[i];
                    sidePrefab = EditorGUILayout.ObjectField(sidePrefab, typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                    if (sidePrefab != null)
                    {
                        mcm.SidePrefab[i] = sidePrefab;
                    }
                    EditorGUI.BeginDisabledGroup(mcm.SideList[i] == null || sidePrefab == null);
                    if (GUILayout.Button("新建一片", GUILayout.Width(90f)))
                    {
                        List<GameObject> SideList = mcm.MapPanel[i].GetComponent<MapInfo>().SideList;
                        GameObject newSide = Instantiate(sidePrefab) as GameObject;
                        newSide.name = "Side" + (SideList.Count + 1);
                        newSide.transform.parent = mcm.SideList[i].transform;
                        newSide.layer = 10;
                        newSide.tag = "Wall";
                        newSide.transform.localPosition = Vector3.zero;
                        SideList.Add(newSide);
                    }
                    EditorGUI.EndDisabledGroup();
                    GUILayout.Label("预设不能为null");
                    GUILayout.EndHorizontal();

                    #region 边界遮挡具体点
                    if (mcm.MonsterPointPanel[i] != null)
                    {
                        List<GameObject> SideList = mcm.MapPanel[i].GetComponent<MapInfo>().SideList;
                        for (int mp = 0; mp < SideList.Count; mp++)
                        {
                            GUILayout.BeginHorizontal();
                            GUILayout.Space(150);
                            SideList[mp] = EditorGUILayout.ObjectField(SideList[mp], typeof(GameObject), true, GUILayout.Width(100)) as GameObject;
                            EditorGUI.BeginDisabledGroup(SideList[mp] == null);
                            {
                                if (GUILayout.Button("移除", GUILayout.Width(50)))
                                {
                                    DestroyImmediate(SideList[mp]);
                                    SideList.RemoveAt(mp);
                                };
                            }
                            EditorGUI.EndDisabledGroup();
                            GUILayout.EndHorizontal();
                        }
                    }
                    #endregion

                    #endregion

                    #region 怪物出生点
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("怪物出现组群:", GUILayout.Width(100));
                    mcm.MonsterPointPanel[i] = EditorGUILayout.ObjectField(mcm.MonsterPointPanel[i], typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                    EditorGUI.BeginDisabledGroup(mcm.MonsterPointPanel[i] == null);
                    if (GUILayout.Button("新建一组", GUILayout.Width(90f)))
                    {
                        //  MapInfo mapInfo = mcm.MapPanel[i].GetComponent<MapInfo>();
                        List<Transform> monsterPointsGroup = mapInfo.MonsterPoints;
                        List<Transform> monsterPointsList = mapInfo.MonsterGroupList;
                        GameObject newMonsterPonintGrop = new GameObject("MonsterPointGrop" + (monsterPointsGroup.Count + 1));

                        newMonsterPonintGrop.transform.parent = mcm.MonsterPointPanel[i].transform;
                        newMonsterPonintGrop.transform.localPosition = Vector3.zero;
                        monsterPointsGroup.Add(newMonsterPonintGrop.transform);
                    }
                    EditorGUI.EndDisabledGroup();
                    GUILayout.EndHorizontal();



                    #region 怪物出生具体点
                    if (mcm.MonsterPointPanel[i] != null)
                    {
                        //  MapInfo mapInfo = mcm.MapPanel[i].GetComponent<MapInfo>();
                        List<Transform> monsterPoints = mapInfo.MonsterPoints;
                        List<Transform> monsterPointsList = mapInfo.MonsterGroupList;
                        List<GameObject> triggerGameObjcet = mapInfo.TriggerForMonsterSpwanGameObject;
                        List<int> monsterID = mapInfo.monsterID;
                        List<int> monsterDropID = mapInfo.monsterDropID;
                        int nowIndex = 0;
                        for (int mp = 0; mp < monsterPoints.Count; mp++)
                        {
                            if (monsterPoints[mp].childCount > 0)
                            {
                                int co = monsterPointsList.Count;
                                if (co <= mp)
                                {
                                    for (int mps = 0; mps < monsterPoints[mp].childCount; mps++)
                                    {
                                        monsterPointsList.Add(monsterPoints[mp].GetChild(mps));
                                    }
                                }
                            }



                            GUILayout.BeginHorizontal();
                            GUILayout.Label("触发刷怪的间隔时间", GUILayout.Width(100));
                            if (mapInfo.MonsterSpawnTime.Count <= mp) { mapInfo.MonsterSpawnTime.Add(0); }
                            mapInfo.MonsterSpawnTime[mp] = EditorGUILayout.FloatField(mapInfo.MonsterSpawnTime[mp], GUILayout.Width(40));
                            GUILayout.Space(10);
                            if (mcm.ListMonsterSpawnEnum[i] == Def.MonsterSpawnEnum.OnTrigger || mcm.ListMonsterSpawnEnum[i] == Def.MonsterSpawnEnum.OnTriggerAndOther)
                            {
                                GUILayout.Label("触发刷怪的物体", GUILayout.Width(90));
                                if (triggerGameObjcet.Count > mp)
                                {
                                    triggerGameObjcet[mp] = EditorGUILayout.ObjectField(triggerGameObjcet[mp], typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                                }
                                else
                                {
                                    triggerGameObjcet.Add(null);
                                    if (triggerGameObjcet.Count > 0 && mp > 0 && triggerGameObjcet[mp - 1] != null)
                                    {
                                        triggerGameObjcet[mp] = triggerGameObjcet[mp - 1];
                                    }

                                }
                            }
                            GUILayout.Label("  出怪组" + (mp + 1), GUILayout.Width(60));
                            monsterPoints[mp] = EditorGUILayout.ObjectField(monsterPoints[mp], typeof(Transform), true, GUILayout.Width(150)) as Transform;



                            GUILayout.Label("  生成个数", GUILayout.Width(60));
                            counts = EditorGUILayout.IntField(counts, GUILayout.Width(50));
                            GUILayout.Label("  X宽度间距", GUILayout.Width(70));
                            xSpace = EditorGUILayout.IntField(xSpace, GUILayout.Width(50));
                            GUILayout.Label("  Z长度间距", GUILayout.Width(70));
                            zSpace = EditorGUILayout.IntField(zSpace, GUILayout.Width(50));
                            GUILayout.Label("  默认ID", GUILayout.Width(60));
                            monsterId = EditorGUILayout.IntField(monsterId, GUILayout.Width(50));
                            GUILayout.Label("  默认掉包组ID", GUILayout.Width(60));
                            monsterDropId = EditorGUILayout.IntField(monsterDropId, GUILayout.Width(50));
                            EditorGUI.BeginDisabledGroup(counts <= 0);
                            if (GUILayout.Button("生成出生点", GUILayout.Width(80)))
                            {
                                //if (triggerGameObjcet[mp] != null)
                                //{
                                //    triggerGameObjcet[mp] = Instantiate(triggerGameObjcet[mp]) as GameObject;
                                //    triggerGameObjcet[mp].transform.parent = monsterPoints[mp].parent;
                                //    triggerGameObjcet[mp].transform.localPosition = Vector3.zero;
                                //    triggerGameObjcet[mp].name = "Trigger" + mp;
                                //    monsterPoints[mp].parent = triggerGameObjcet[mp].transform;
                                //}
                                for (int msc = 0; msc < counts; msc++)
                                {
                                    GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                                    go.name = "Poin" + msc;
                                    go.transform.parent = monsterPoints[mp];
                                    go.transform.localPosition = Vector3.zero;
                                    if ((msc) % 2 == 0)
                                    {
                                        go.transform.localPosition = new Vector3((msc) * xSpace, 0, 0);
                                    }
                                    else
                                    {
                                        go.transform.localPosition = new Vector3((msc - 1) * xSpace, 0, zSpace);
                                    }
                                    monsterPointsList.Add(go.transform);
                                    monsterID.Add(monsterId);
                                    monsterDropID.Add(monsterDropId);
                                }
                                counts = 0;
                            }
                            EditorGUI.EndDisabledGroup();

                            EditorGUI.BeginDisabledGroup(monsterPointsList.Count <= mp);
                            if (GUILayout.Button("清除", GUILayout.Width(80)))
                            {

                                int start = 0;
                                int number = 0;
                                for (int a = 0; a < mp; a++)
                                {
                                    start += monsterPoints[a].childCount;
                                }
                                number = monsterPoints[mp].childCount;

                                for (int b = 0; b < number; b++)
                                {
                                    monsterID.RemoveAt(b + start);
                                    monsterDropID.RemoveAt(b + start);
                                    monsterPointsList.RemoveAt(b + start);
                                    start -= 1;
                                }
                                //monsterPointsList.RemoveAt(mp);
                                DestroyImmediate(monsterPoints[mp].gameObject);
                                monsterPoints.RemoveAt(mp);
                                //  DestroyImmediate(triggerGameObjcet[mp]);
                                triggerGameObjcet.RemoveAt(mp);
                                return;
                            }
                            EditorGUI.EndDisabledGroup();


                            GUILayout.EndHorizontal();

                            for (int mps = 0; mps < monsterPoints[mp].childCount; mps++)
                            {
                                GUILayout.BeginHorizontal();
                                GUILayout.Space(200);
                                monsterPointsList[nowIndex] = EditorGUILayout.ObjectField(monsterPointsList[nowIndex], typeof(Transform), true, GUILayout.Width(150)) as Transform;
                                GUILayout.Label("怪物ID", GUILayout.Width(40));
                                if (monsterDropID.Count <= nowIndex) { monsterDropID.Add(0); }
                                monsterID[nowIndex] = EditorGUILayout.IntField(monsterID[nowIndex], GUILayout.Width(70));
                                GUILayout.Label("掉包组ID", GUILayout.Width(60));
                                monsterDropID[nowIndex] = EditorGUILayout.IntField(monsterDropID[nowIndex], GUILayout.Width(70));
                                GUILayout.EndHorizontal();
                                nowIndex += 1;
                            }
                        }
                    }
                    #endregion

                    #endregion

                    #region 其他物品的顶层
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("其他物品的顶层:", GUILayout.Width(100));
                    mcm.OtherGameObject[i] = EditorGUILayout.ObjectField(mcm.OtherGameObject[i], typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                    GUILayout.Label("输入需要添加物品的名字:", GUILayout.Width(140));
                    otherGameObjectName = GUILayout.TextField(otherGameObjectName, GUILayout.Width(100));
                    GUILayout.Label("添加物预设", GUILayout.Width(80f));
                    GameObject otherGameObjectPrefab = mcm.OtherGameObjectPrefab[i];
                    otherGameObjectPrefab = EditorGUILayout.ObjectField(otherGameObjectPrefab, typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                    if (otherGameObjectPrefab != null)
                    {
                        mcm.OtherGameObjectPrefab[i] = otherGameObjectPrefab;
                    }

                    EditorGUI.BeginDisabledGroup(mcm.MonsterPointPanel[i] == null || string.IsNullOrEmpty(otherGameObjectName) || otherGameObjectPrefab == null);
                    if (GUILayout.Button("新建一个", GUILayout.Width(90f)))
                    {
                        List<GameObject> OtherGameObject = mcm.MapPanel[i].GetComponent<MapInfo>().OtherGameObjectList;
                        List<Def.GameObjectTypeEnum> otherGameObjectType = mcm.MapPanel[i].GetComponent<MapInfo>().OtherGameObjectType;
                        GameObject newOtherGameObject = Instantiate(otherGameObjectPrefab) as GameObject;
                        newOtherGameObject.name = otherGameObjectName;
                        newOtherGameObject.transform.parent = mcm.OtherGameObject[i].transform;
                        newOtherGameObject.transform.localPosition = Vector3.zero;
                        OtherGameObject.Add(newOtherGameObject);
                        otherGameObjectType.Add(Def.GameObjectTypeEnum.NPC);
                    }
                    EditorGUI.EndDisabledGroup();
                    GUILayout.EndHorizontal();

                    #region 其他具体物品
                    if (mcm.MonsterPointPanel[i] != null)
                    {
                        List<GameObject> otherGameObject = mcm.MapPanel[i].GetComponent<MapInfo>().OtherGameObjectList;
                        List<Def.GameObjectTypeEnum> otherGameObjectType = mcm.MapPanel[i].GetComponent<MapInfo>().OtherGameObjectType;
                        for (int mp = 0; mp < otherGameObject.Count; mp++)
                        {
                            GUILayout.BeginHorizontal();
                            GUILayout.Space(150);
                            otherGameObject[mp] = EditorGUILayout.ObjectField(otherGameObject[mp], typeof(GameObject), true, GUILayout.Width(150)) as GameObject;
                            GUILayout.Label("物品类型", GUILayout.Width(50));
                            if (otherGameObjectType.Count >= mp + 1)
                            {
                                otherGameObjectType[mp] = (Def.GameObjectTypeEnum)EditorGUILayout.EnumPopup(otherGameObjectType[mp], GUILayout.Width(100));
                            }
                            else
                            {
                                otherGameObjectType.Add(Def.GameObjectTypeEnum.NPC);
                            }
                            if (otherGameObjectType[mp] == Def.GameObjectTypeEnum.MonsterSpawnNow)
                            {
                                otherGameObject[mp].layer = Def.GameObjectLayer.MonsterSpawn;
                                otherGameObject[mp].tag = "MonsterSpawnNow";
                                GUILayout.Label("绑定关闭边界", GUILayout.Width(70));
                                if (mapInfo.TriggerMonsterSpawnBingClose.Count > mp)
                                {
                                    mapInfo.TriggerMonsterSpawnBingClose[mp] = EditorGUILayout.ObjectField(mapInfo.TriggerMonsterSpawnBingClose[mp], typeof(GameObject), true, GUILayout.Width(100)) as GameObject;
                                }
                                else
                                {
                                    mapInfo.TriggerMonsterSpawnBingClose.Add(null);
                                }
                                GUILayout.Label("绑定打开边界", GUILayout.Width(70));
                                if (mapInfo.TriggerMonsterSpawnBingOpen.Count > mp)
                                {
                                    mapInfo.TriggerMonsterSpawnBingOpen[mp] = EditorGUILayout.ObjectField(mapInfo.TriggerMonsterSpawnBingOpen[mp], typeof(GameObject), true, GUILayout.Width(100)) as GameObject;
                                }
                                else
                                {
                                    mapInfo.TriggerMonsterSpawnBingOpen.Add(null);
                                }
                                EditorGUI.BeginDisabledGroup(otherGameObject[mp] == null);
                                if (GUILayout.Button("绑定", GUILayout.Width(50)))
                                {
                                    if (otherGameObject[mp].GetComponent<TriggerMonsterSpawn>() == null)
                                    {
                                        otherGameObject[mp].AddComponent<TriggerMonsterSpawn>();
                                    }
                                }
                                EditorGUI.EndDisabledGroup();
                            }
                            else if (otherGameObjectType[mp] == Def.GameObjectTypeEnum.NPC)
                            {
                                otherGameObject[mp].tag = "NPC";
                            }
                            else if (otherGameObjectType[mp] == Def.GameObjectTypeEnum.BreakOutTheProps)
                            {
                                otherGameObject[mp].layer = Def.GameObjectLayer.MonsterLayer;
                                EditorGUI.BeginDisabledGroup(otherGameObject[mp] == null);
                                breadOutId = EditorGUILayout.IntField(breadOutId, GUILayout.Width(70));
                                if (GUILayout.Button("生成", GUILayout.Width(50)))
                                {
                                    if (otherGameObject[mp].GetComponent<BreakOutItems>() == null)
                                    {
                                        BreakOutItems ites = otherGameObject[mp].AddComponent<BreakOutItems>();
                                        ites.DropOutId = breadOutId;
                                    }
                                }
                                EditorGUI.EndDisabledGroup();
                            }
                            else if (otherGameObjectType[mp] == Def.GameObjectTypeEnum.BossTargetPositon)
                            {
                                GUILayout.Label("Boss位移到的地方", GUILayout.Width(170));
                                mapInfo.BossTargertPositon = EditorGUILayout.ObjectField(mapInfo.BossTargertPositon, typeof(Transform), true, GUILayout.Width(170)) as Transform;
                            }
                            EditorGUI.BeginDisabledGroup(otherGameObject[mp] == null);
                            if (GUILayout.Button("移除", GUILayout.Width(50)))
                            {
                                DestroyImmediate(otherGameObject[mp]);
                                otherGameObject.RemoveAt(mp);
                                otherGameObjectType.RemoveAt(mp);
                            }
                            EditorGUI.EndDisabledGroup();
                            GUILayout.EndHorizontal();
                        }
                    }
                    #endregion

                    #endregion
                }
                #region 刷新
                if (GUILayout.Button("刷新", GUILayout.Width(140f)))
                {
                    //   MapInfo mapInfo = mcm.MapPanel[i].GetComponent<MapInfo>();
                    Reflash(mapInfo, i);
                }
                #endregion
                GUILayout.Space(20);
            }
        }
        EditorGUILayout.EndScrollView();
        #region 功能按钮
        GUILayout.BeginVertical();
        if (GUILayout.Button("删除存储数据", GUILayout.Width(140f)))
        {
            for (int c = 0; c < mcm.MapPanel.Count; c++)
            {
                DestroyImmediate(mcm.MapPanel[c]);
            }
            DestroyImmediate(MissionInfo);
            OnEnable();//刷新
        }

        if (GUILayout.Button("保存存储数据", GUILayout.Width(140f)))
        {
            //PrefabUtility.CreatePrefab(path, MissionInfo);
        }

        if (GUILayout.Button("应用关卡配置", GUILayout.Width(140f)))
        {
            //PrefabUtility.CreatePrefab(path, MissionInfo);
        }
        GUILayout.EndVertical();
        #endregion
        #endregion

    }

    private static void Reflash(MapInfo mapInfo, int lastIndex)
    {
        //int lastIndex = info.MapID.Count - 1;
        mapInfo.MapID = info.MapID[lastIndex];                                        //关卡ID
        mapInfo.FightingType = info.ListFightingType[lastIndex];           //战斗类型
        mapInfo.MapStege = info.MapStege[lastIndex];                              //地图模型
        mapInfo.MonsterPointPanel = info.MonsterPointPanel[lastIndex].gameObject; //出怪点顶层
        mapInfo.OtherGameObjcetPanel = info.OtherGameObject[lastIndex];              //其他物品顶层
        mapInfo.RolePoint = info.ListPointTranfrom[lastIndex];                                     //人物出生点
        mapInfo.SidePanel = info.SideList[lastIndex];                                               //边界顶层
        mapInfo.MonsterSpawnType = info.ListMonsterSpawnEnum[lastIndex];

    }
    private static void AddItem(GameObject map, string btnName, List<GameObject> list, int index)
    {
        //GUILayout.BeginHorizontal();
        //bool show = VampireEditorTools.DrawPrefixButton(btnName);
        GUILayout.Label(btnName, GUILayout.Width(100f));
        Object o = EditorGUILayout.ObjectField(map, typeof(GameObject), true, GUILayout.Width(150f)) as Object;
        //if (o != null)
        //{
        //    Object mb = o;
        //    if (Selection.activeObject != mb && GUILayout.Button("转到", GUILayout.Width(40f)))
        //        Selection.activeObject = mb;
        //}
        //if (o != null && GUILayout.Button("X", GUILayout.Width(20f)))
        //{
        //    o = null;
        //}
        list[index] = o as GameObject;
        //GUILayout.EndHorizontal();
    }
    private void SaveDate()
    {
        if (mcm.MapStege != null && mcm.MapStege.Count > 0)
        {
            if (!AssetDatabase.Contains(mcm))
            {
                AssetDatabase.DeleteAsset(path);
                MapConfigModel clone = mcm.Clone() as MapConfigModel;
                AssetDatabase.CreateAsset(clone, path);
            }
        }

        Debug.Log("已经保存数据");
    }

    /// <summary>
    /// Draw details about the specified object in column format.
    /// </summary>

}
